<?php

namespace Iyting\Tiktok\MiniProgram\Auth;
use GuzzleHttp\Psr7\Utils;
use Psr\Http\Message\RequestInterface;
use Iyting\Tiktok\MiniProgram\Kernel\AccessToken as BaseAccessToken;

class ClientToken extends BaseAccessToken
{

    protected  $baseUri = 'https://open.douyin.com/';
    /**
     * @var string
     */
    protected $endpointToGetToken = 'oauth/client_token/';


    /**
     * @var string
     */
    protected $tokenKey = 'access_token';

    /**
     * @var string
     */
    protected $cachePrefix = 'EasyTiktok.mini_program.client_token.';

    protected function getCredentials(): array
    {
  
        return [
            'client_key'      => $this->app['config']['app_id'],
            'client_secret'     => $this->app['config']['secret'],
            'grant_type' => 'client_credential',
        ];
    }


    public function applyToPostRequest(RequestInterface $request, array $requestOptions = []): RequestInterface {


        $query = $request->getBody()->getContents();
        $request->getBody()->rewind();
        $query = \GuzzleHttp\json_decode($query, true);
        //$query = array_merge($this->getQuery(), $query);
        $accessToken = $this->getQuery();

        return $request->withAddedHeader('access-token',$accessToken['access_token'])
            ->withBody(Utils::streamFor(json_encode($query)));
    }

}